﻿using DataHelper;
using JKYGL.Models;
using Microsoft.AspNetCore.Mvc;

namespace JKYGL.Controllers
{
    public class RIOController : Controller
    {
        [HttpPost]
       public JsonResult SaveTicketToServer(string tKey,string tValue)
        {
            var ticket = new CaptchaTickets() { Id = 0, TicketName = tKey, TicketValue = tValue, CreateTime = DateTime.Now };
            string sqlstr = $"insert into CaptchaTicket (TicketName,TicketValue,CreateTime) values ('{ticket.TicketName}','{ticket.TicketValue}','{ticket.CreateTime.ToString()}')";
            //sqlstr = "create table CaptchaTicket (Id int identity(1,1), TicketName nvarchar(10),TicketValue nvarchar(1024),CreateTime datetime);";
            var re = SqlHelper.ExecuteNonQueryBySQL(sqlstr);

            return Json(new {Result= "success",M= ticket });
        }
        [HttpPost]
        public JsonResult GetTicketFromServer(string tKey)
        {
            var ticket = new CaptchaTickets();
            string sqlstr = $"delete from CaptchaTicket where datediff(SECOND,CreateTime,getdate())>120;select top 1 * from CaptchaTicket order by CreateTime asc;";
            var ds = SqlHelper.GetDataBySQL(sqlstr);
            if (ds.Tables[0].Rows.Count>0)
            {
                ticket.Id = (int)ds.Tables[0].Rows[0]["Id"];
                ticket.TicketName = ds.Tables[0].Rows[0]["TicketName"].ToString()??"unKnown";
                ticket.TicketValue = ds.Tables[0].Rows[0]["TicketValue"].ToString() ?? "unKnown";
                ticket.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString()??"1999-01-01");
                sqlstr = $"delete from CaptchaTicket where id='{ticket.Id}'";
                SqlHelper.ExecuteNonQueryBySQL(sqlstr);

            }

            return Json(new { Result = "success", M = ticket });
        }
      
    }
}
